﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using HtmlAgilityPack;

namespace BlogTruyen.ViewModels
{
    public class Reading : Models.BaseModel
    {
        public string chapter_name { get; set; }
        private List<Img> _items { get; set; }
        public ObservableCollection<Img> items { get; set; }
        public bool isLoaded { get; set; }
        public string content { get; set; }

        public Reading()
        {
            items = new ObservableCollection<Img>();
        }

        public void LoadHtml(string html)
        {
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);

            var articleNode = doc.DocumentNode.SelectSingleNode("//article[@id='content']");
            content = articleNode.InnerHtml;
            var imgNodes = articleNode.SelectNodes("./img");
            int i = 1;

            //_items = imgNodes.Select(t => new Img
            //{
            //    index = String.Format("{0}/{1}", i++, imgNodes.Count),
            //    src = t.GetAttributeValue("src", ""),
            //}).ToList();
            //items.Add(_items[0]);
            //items.Add(_items[1]);
            if (imgNodes==null) return;

            foreach (HtmlNode imgNode in imgNodes)
            {
                items.Add(new Img
                {
                    id=i,
                    index = String.Format("{0}/{1}", i++, imgNodes.Count),
                    src = imgNode.GetAttributeValue("src", ""),
                });
            }

            if (items.Count > 0) items.First().isHide = false;
            NotifyPropertyChanged("chapter_name");
            isLoaded = true;
        }

        internal void LoadNextImage(int id)
        {
            //var prev = id - 1;
            //var next = id + 1;
            //var prev1 = id - 2;
            //var next1 = id + 2;
            //if (prev > 0) items.FirstOrDefault(t => t.id == prev).isHide = false;
            //if (next <= items.Count) items.FirstOrDefault(t => t.id == next).isHide = false;
            //if (prev1 > 0) items.FirstOrDefault(t => t.id == prev1).isHide = true;
            //if (next1 <= items.Count) items.FirstOrDefault(t => t.id == next1).isHide = true;
        }
    }
}
